package com.fh.modules.oas.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.system.query.QueryGenerator;
import com.fh.common.util.ConvertUtils;
import com.fh.common.util.DateUtils;
import com.fh.modules.oas.entity.OasTimesheet;
import com.fh.modules.oas.model.OasTimesheetModel;
import com.fh.modules.oas.service.IOasTimesheetService;
import com.fh.modules.oas.vo.OasTimesheetVo;
import com.fh.modules.shiro.ShiroUtils;
import com.fh.modules.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @Title: Controller
 * @Description: 工作日报
 * @author： code generator
 * @date： 2019-12-04
 * @version： V1.0
 */
@RestController
@RequestMapping("/oas/oasTimesheet")
@Slf4j
@Api("工作日报")
public class OasTimesheetController {
    @Autowired
    private IOasTimesheetService oasTimesheetService;

    /**
     * 分页列表查询
     *
     * @param oasTimesheet
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "工作日报分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(OasTimesheet oasTimesheet,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        oasTimesheet.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapper<OasTimesheet> queryWrapper = QueryGenerator.initQueryWrapper(oasTimesheet, req.getParameterMap());
        Page<OasTimesheet> page = new Page<OasTimesheet>(pageNo, pageSize);
        IPage<OasTimesheet> pageList = oasTimesheetService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @GetMapping(value = "/my")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "工作日报分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryMyTimesheet(OasTimesheet oasTimesheet,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        oasTimesheet.setDelFlag(CommonConstant.DEL_FLAG_0);
        oasTimesheet.setUserId(ShiroUtils.getUser().getId());
        QueryWrapper<OasTimesheet> queryWrapper = QueryGenerator.initQueryWrapper(oasTimesheet, req.getParameterMap());
        Page<OasTimesheet> page = new Page<OasTimesheet>(pageNo, pageSize);
        IPage<OasTimesheet> pageList = oasTimesheetService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param oasTimesheet
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation("添加")
    @AutoLog("添加工作日报")
    public Result add(@RequestBody OasTimesheet oasTimesheet) {
        SysUser sysUser = ShiroUtils.getUser();
        oasTimesheet.setJobDate(DateUtils.formatDate());
        oasTimesheet.setUserId(sysUser.getId());
        oasTimesheet.setUserName(sysUser.getRealname());
        oasTimesheetService.save(oasTimesheet);
        return Result.ok("添加成功");
    }

    /**
     * 编辑
     *
     * @param oasTimesheet
     * @return
     */
    @PutMapping(value = "/edit")
    @ApiOperation("修改")
    @AutoLog("修改工作日报")
    public Result edit(@RequestBody OasTimesheet oasTimesheet) {
        boolean ok = oasTimesheetService.updateById(oasTimesheet);
        return ok ? Result.ok("修改成功") : Result.error("修改失败");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("通过id删除")
    @AutoLog("删除工作日报")
    public Result delete(@RequestParam(name = "id", required = true) String id) {
        oasTimesheetService.deleteEntityByDelFlag(id);
        return Result.ok("删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @ApiOperation("批量删除")
    @AutoLog("批量删除工作日报")
    public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        oasTimesheetService.deleteBatchEntityByDelFlag(ids);
        return Result.ok("批量删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @ApiOperation("通过id查询")
    @AutoLog(value = "工作日报通过id查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryById(@RequestParam(name = "id", required = true) String id) {
        OasTimesheet oasTimesheet = oasTimesheetService.getById(id);
        return Result.ok(oasTimesheet);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportXls")
    @ApiOperation("导出excel")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        // Step.1 组装查询条件
        OasTimesheetVo oasTimesheetVo=new OasTimesheetVo();
        try {
            String paramsStr = request.getParameter("paramsStr");
            if (ConvertUtils.isNotEmpty(paramsStr)) {
                String deString = URLDecoder.decode(paramsStr, "UTF-8");
                oasTimesheetVo = JSON.parseObject(deString, OasTimesheetVo.class);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<OasTimesheetModel> pageList = oasTimesheetService.queryPageList(oasTimesheetVo);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "工作日报列表");
        mv.addObject(NormalExcelConstants.CLASS, OasTimesheetModel.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("工作日报列表数据", "导出人:系统管理员", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }


}
